package com.gtq.uhome.controller;


import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.DistributionPortSerVice;
import com.gtq.uhome.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 配送端口 模块
 */
@Controller
@RequestMapping(value = "/distributionPort")
@ResponseBody
@CrossOrigin
@Api(tags = "配送端口模块 首页接口说明文档 咨询李龙")
public class DistributionPortController {
    @Resource
    DistributionPortSerVice distributionPortSerVice;

    /**
     * 配送端口首页-->查询未派送的订单
     *
     * @return 返回成功与否
     */
    @PostMapping
    @ApiOperation(value = "配送端口首页-->查询未派送的订单", notes = "调式有问题，请咨询", response = Map.class, httpMethod = "POST")
    public Result selectUndelivered(@RequestBody Map<String ,Integer> map) {
        Integer courierId = map.get("courierId");
        Map<String,Object> orderVos = distributionPortSerVice.selectUndelivered(courierId);
        if (orderVos!=null) {
            return Result.ok(orderVos, "查询成功");
        }
        return Result.ok(null, "暂时没有可以派送的订单,请稍后再试");
    }

    /**
     * 配送端口首页-->根据站点查询未派送的订单
     *
     * @return 返回成功与否
     */
    @PostMapping("/selectBySiteName")
    @ApiOperation(value = "配送端口首页-->通过站点查询未派送的订单", notes = "字段如下：siteName:站点名字,调式有问题，请咨询", response = Map.class, httpMethod = "POST")
    @ApiImplicitParam(name = "siteName", value = "站点名字", defaultValue = "武侯店", dataType = "String", required = true, paramType = "body")
    public Result selectUndeliveredBySite(@RequestBody Map siteName) {
        String siteName1 = (String) siteName.get("siteName");
        //打印获取值
        System.out.println("站点名字：" + siteName1);
        //调用查询方法
        List<OrderVo> orderVos = distributionPortSerVice.selectUndeliveredBySite(siteName1);
        //判断结果是否有
        if (orderVos.size() > 0) {
            return Result.ok(orderVos, "查询成功");
        }
        return Result.ok(null, "暂时没有可以派送的订单,请稍后再试");
    }

    /**
     * 配送端口首页-->派送员抢单
     *
     * @return 返回抢单状态
     */
    @PostMapping("/setReceivingOrders")
    @ApiOperation(value = "配送端口首页-->派送员抢单", notes = "字段如下：courierId:派送员ID（登录成功后，会返回一个用户ID)，orderId：订单号", response = Map.class, httpMethod = "POST")
    @ApiImplicitParam(name = "order", value = "post请求，装的字段", defaultValue = "null", dataType = "String", required = true, paramType = "body")
    public Result setReceivingOrders(@RequestBody Map<String, Object> map) {
        //接收值
        Integer courierId = (Integer) map.get("courierId");
        String orderId = (String) map.get("orderId");
        //调用派送员抢单方法
        Boolean aBoolean = distributionPortSerVice.setReceivingOrders(courierId, orderId);
        //判断
        if (aBoolean) {
            return Result.ok(null, "抢单成功");
        }
        return Result.ok("暂时没有可以派送的订单,请稍后再试");
    }

    /**
     * 配送端口首页-->派送员开启/停止接单
     *
     * @return 返回接单状态
     */
    @PostMapping("/receivingOrders")
    @ApiOperation(value = "配送端口首页-->派送员开启/停止接单", notes = "字段如下：courierId:派送员ID（登录成功后，会返回一个用户ID）courierState:派送员状态（接单和停止接单，点击接单按钮后变成停止接单）", response = Map.class, httpMethod = "POST")
    @ApiImplicitParam(name = "map", value = "post请求，装的字段courierState:派送员状态", defaultValue = "null", dataType = "String", required = true, paramType = "body")
    public Result receivingOrders(@RequestBody Map<String, Object> map) {
        //接收值
        Integer courierId = (Integer) map.get("courierId");
        String courierState = (String) map.get("courierState");

        //调用派送员抢单方法
        Boolean aBoolean = distributionPortSerVice.receivingOrders(courierId, courierState);
        //判断
        if (aBoolean) {
            return Result.ok(null, courierState + "成功");
        }
        return Result.ok(courierState + "失败，请联系客服");
    }


}
